﻿using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

/// <summary>
/// GetWeiXinURL 的摘要说明
/// </summary>
public class GetWeiXinURL
{
    private string sogoustr = "http://weixin.sogou.com/weixinwap?query=%E7%88%B1%E5%BF%83%E6%9C%8D%E5%8A%A1%E7%A4%BE&type=1&ie=utf8&_sug_=y&_sug_type_=&s_from=input";
    private string theTime = "";
    private string theUrl = "";
    private string feedTime = "";
    private DataSet ds = new DataSet();
    DataTable dt;
    public GetWeiXinURL()
    {
        ds.ReadXml(HttpContext.Current.Server.MapPath("~/Config/WeiXinURL.config"));
        dt = ds.Tables[0];
        this.theTime = dt.Rows[0]["theTime"].ToString();
        this.theUrl= dt.Rows[0]["theUrl"].ToString();
        this.feedTime= dt.Rows[0]["feedTime"].ToString();
    }
    public string getTheTime()
    {
        return this.theTime;
    }
    public string getUrl()
    {
        string _url = "";
        DateTime now = DateTime.Now;
        //TimeSpan ts = now - DateTime.Parse(dt.Rows[0]["theTime"].ToString());
        TimeSpan ts = now - DateTime.Parse(this.getTheTime());
        if (ts.TotalMinutes > 60)
        {
      
            string html = HtmlTools.GetWebHtml.GetHtml(sogoustr, "UTF-8");
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(html);  //加载html
            HtmlNode parent = doc.DocumentNode;
            HtmlNodeCollection gzh_name = parent.SelectNodes("//*[@class=\"gzh-name\"]");
            foreach (HtmlNode note in gzh_name)
            {
                if(note.InnerText.Equals("微信号：jyaxfws"))
                {
                    HtmlNode gzh_box = note.ParentNode.ParentNode.ParentNode;
                    _url=gzh_box.SelectSingleNode("//*[@data-uigs=\"account_title_4\"]").Attributes["href"].Value;
                    _url = HttpUtility.HtmlDecode(_url);
                    _url = _url.Replace("amp;","");
                    break;
                }
            }
            dt.Rows[0]["theTime"] = DateTime.Now;
            dt.Rows[0]["theUrl"] = _url;
            ds.WriteXml(HttpContext.Current.Server.MapPath("~/Config/WeiXinURL.config"));
            return _url;
        }
        else
        {
            return this.theUrl;
        }
    }
    public string getFeedTime()
    {
        return this.feedTime;
    }
    public void setFeedTime(string time)
    {
        dt.Rows[0]["feedTime"] = time;
        ds.WriteXml(HttpContext.Current.Server.MapPath("~/Config/WeiXinURL.config"));
    }
    //public bool setTime(string time)
    //{
    //    dt.Rows[0]["theTime"] = time;
    //    try
    //    {
    //        ds.WriteXml(HttpContext.Current.Server.MapPath("~/Config/WeiXinURL.config"));
    //        return true;
    //    }
    //    catch
    //    {
    //        return false;
    //    }

    //}
    //public bool setUrl(string url)
    //{
    //    dt.Rows[0]["theUrl"] = url;
    //    try
    //    {
    //        ds.WriteXml(HttpContext.Current.Server.MapPath("~/Config/WeiXinURL.config"));
    //        return true;
    //    }
    //    catch
    //    {
    //        return false;
    //    }
    //}
}